package com.etc.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.Comment;
import com.etc.service.CommentService;
import com.etc.util.Message;
import com.etc.util.PageData;

@Controller
@RequestMapping("comapi")
public class CommentController {

	@Resource
	private CommentService cs;

	@Resource(name = "message")
	private Message msg;

	private Logger log = Logger.getLogger(CommentController.class);

	/**
	 * 删除的方法
	 * 
	 * @param comid
	 * @return
	 */
	@RequestMapping(value = "comment/{comid}", method = RequestMethod.DELETE)
	@ResponseBody
	public Message deleteByPrimaryKey(@PathVariable(value = "comid") int comid) {
		boolean flag = cs.deleteByPrimaryKey(comid);
		if (flag) {
			msg.setMsg("删除成功");
		} else {
			msg.setMsg("删除失败");
		}
		return msg;
	}

	/**
	 * 添加的方法
	 * 
	 * @param comment
	 * @return
	 */
	@RequestMapping(value = "comment", method = RequestMethod.POST)
	@ResponseBody
	public Message insertSelective(Comment comment, HttpSession session) {
		comment.setCondate(new Date());
		boolean flag = cs.insertSelective(comment);
		if (flag) {
			msg.setMsg("添加成功");
		} else {
			msg.setMsg("添加失败");
		}
		return msg;
	}

	/**
	 * 查询单个评论的方法
	 * 
	 * @param comment
	 * @return
	 */
	@RequestMapping(value = "comment/{comid}", method = RequestMethod.GET)
	@ResponseBody
	public Comment selectByPrimaryKey(@PathVariable(value = "comid") int comid) {
		Comment comment = cs.selectByPrimaryKey(comid);
		return comment;
	}

	/**
	 * 修改的方法
	 * 
	 * @param comment
	 * @return
	 */
	@RequestMapping(value = "comment", method = RequestMethod.PUT)
	@ResponseBody
	public Message updateByPrimaryKeySelective(@RequestBody Comment comment) {
		boolean flag = cs.insertSelective(comment);
		if (flag) {
			msg.setMsg("修改成功");
		} else {
			msg.setMsg("修改失败");
		}
		return msg;
	}

	@RequestMapping(value = "comment", method = RequestMethod.GET)
	@ResponseBody
	public PageData<Comment> selectByPage(int page, int limit, String content) {
		if (content == null) {
			content = "";
		}
		PageData<Comment> pg = cs.selectByPage(page, limit, content);
		pg.setCode(0);
		pg.setMsg("");
		return pg;
	}

	@RequestMapping(value = "com/{traid}", method = RequestMethod.GET)
	@ResponseBody
	public List<Comment> selectByTraid(@PathVariable(value = "traid") int traid) {
		List<Comment> list = cs.selectByTraid(traid);
		return list;
	}

}
